package com.bulletelc.apollopre.module.user.register;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.EncryptUtil;
import com.bulletelc.apollopre.utils.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * author: lhl
 * date  : 2017/12/16
 * desc  : TODO
 */

public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter {
    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void sendSMS(String mobile_number) {
        JSONObject object = new JSONObject();
        JSONObject objSub = new JSONObject();
        try {
            objSub.put("mobile_number", mobile_number);
            objSub.put("signature_encrypt_data",EncryptUtil.sha256(mobile_number));
            object.put("CaptchaForm", objSub);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String objStr = object.toString();

        RequestBody body = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON),objStr);
        OkGo.<String>post(NetWorkPath.POST_SMS_CAPTCHA)
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseResultSMS(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorSMS(response.getException().toString());
                        }
                    }
                });
    }

    @Override
    public void register(String nickname, String mobile_number, String password,String encrypt, String unionid) {
        JSONObject objForm = new JSONObject();
        JSONObject object = new JSONObject();
        try {
            object.put("form_type", NetWorkPath.APP_FORM_TYPE);
            objForm.put("name", nickname);
            objForm.put("mobile_number", mobile_number);
            objForm.put("password",password);
            objForm.put("signature_encrypt_data", encrypt);
            objForm.put("registration_src_type", 4);
            if (null != unionid) {
                objForm.put("wechat_union_id", unionid);
            }
            object.put("MemberRegistrationForm", objForm);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String objStr = object.toString();

        RequestBody body = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), objStr);

        OkGo.<String>post(NetWorkPath.POST_REGISTER)
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseResult(response);

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onError(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析成功返回的数据  SMS
     *
     * @param response
     */
    private void parseResultSMS(Response<String> response) {
        try {
            org.json.JSONObject obj = new org.json.JSONObject(response.body().toString());
            org.json.JSONObject meta = (org.json.JSONObject) obj.get("meta");
            org.json.JSONObject data = (org.json.JSONObject) obj.get("data");
            String code= (String) meta.get("code");
            if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(code)) {
                String authCode = data.getString("code");
                String encrypt = data.getString("signature_encrypt_data");
                if (isViewAttached()) {
                    LogUtil.i("手机验证码："+authCode);
                    getView().onSuccessSMS(authCode,encrypt);
                }
            }else {
                if (isViewAttached()) {
                    getView().onFailSMS((String) meta.get("msg"));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            if (isViewAttached()) {
                getView().onErrorSMS(MyApplication.getInstance().getString(R.string.toast_json_exception));
            }
        }
    }

    /**
     * 解析成功返回的数据  注册
     *
     * @param response
     */
    private void parseResult(Response<String> response) {
        try {
            org.json.JSONObject obj = new org.json.JSONObject(response.body().toString());
            org.json.JSONObject meta = (org.json.JSONObject) obj.get("meta");
            String code= (String) meta.get("code");
            if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(code)) {
                if (isViewAttached()) {
                    getView().onSuccess();
                }
            }else {
                if (isViewAttached()) {
                    getView().onFail((String) meta.get("msg"));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            if (isViewAttached()) {
                getView().onError(MyApplication.getInstance().getString(R.string.toast_json_exception));
            }
        }
    }


}
